package madixin.t1094;

class Solution {
    public boolean carPooling(int[][] trips, int capacity) {
        int[] diffs = new int[1000];

        // 生成diff差分数组
        for (int trip[] : trips) {
            diffs[trip[1]] += trip[0];
            if (trip[2]< 1000) {
                diffs[trip[2]] -= trip[0];
            }
        }

        // 还原成原始数组
        int curPassengers = 0;
        for (int diff : diffs) {
            curPassengers += diff;
            if (curPassengers > capacity) {
                return false;
            }
        }

        return true;
    }

    public static void main(String[] args) {
        Solution s = new Solution();


        int[][] trips1 = new int[][]{{2, 1, 5}, {3, 3, 7}};
        System.out.println(s.carPooling(trips1, 4));

        int[][] trips2 = new int[][]{{2, 1, 5}, {3, 3, 7}};
        System.out.println(s.carPooling(trips2, 5));


        int[][] trips3 = new int[][]{{9,0,1}, {3,3,7}};
        System.out.println(s.carPooling(trips3, 4));

    }
}
